class Article < ActiveRecord::Base
  connection.execute "SET NAMES UTF8"
  belongs_to :user
  
  def self.save_file(file_params)
    file_url = file_params.original_filename
    save_path = "#{RAILS_ROOT}/public/files"
    FileUtils.makedirs(save_path) if !File.exist?(save_path)
    file_name = file_url.gsub("\\","/").split("/").last
    split_file_name_and_type = file_name.split(".")
    if split_file_name_and_type.length < 2
      return
    end
    file_name = split_file_name_and_type[0]+Time.now.strftime('%y%m%d%H%M%S')+'.'+split_file_name_and_type.last
    target_path = File.join(save_path,file_name)
    f = File.new(target_path, "wb")
    f.write file_params.read
    f.close
    return file_name
  end
end
